package com.example.parttimejob.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.parttimejob.mapper.PartTimeJobsMapper;
import com.example.parttimejob.mapper.ReviewsMapper;
import com.example.parttimejob.mapper.StoreMapper;
import com.example.parttimejob.mapper.UserMapper;
import com.example.parttimejob.pojo.entity.Merchants;
import com.example.parttimejob.pojo.entity.PartTimeJobs;
import com.example.parttimejob.pojo.entity.Reviews;
import com.example.parttimejob.pojo.entity.User;
import com.example.parttimejob.pojo.entity.dto.ChangeReviewDto;
import com.example.parttimejob.pojo.entity.dto.UserAddReviewDto;
import com.example.parttimejob.pojo.entity.dto.UserDto;
import com.example.parttimejob.pojo.entity.dto.UserModifyDto;
import com.example.parttimejob.pojo.entity.vo.*;
import com.example.parttimejob.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceimpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PartTimeJobsMapper partTimeJobsMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private ReviewsMapper reviewsMapper;

    //注册
    @Override
    public String registUser(String username, String passwd) {
        User user=userMapper.selectByUsername(username);
        if(user!=null) {
            return "用户名已存在!";
        }
        else {
            if (passwd != null) {
                String status = "未实名";
                userMapper.registUser(username, passwd, status);
                return "注册成功";
            } else {
                return "密码不能为空";
            }
        }
    }

    //登录
    @Override
    public int loginUser(String username, String passwd) {
        User user=userMapper.selectByUsername(username);
        String password;
        if(user==null) {
            //用户名不存在
           return 0;
        }
        else {
            password = user.getPasswd();
        }
        if(password==null){
            //请输入正确的用户名和密码
            return -1;
        }
        if(password.equals(passwd)){
//            System.out.println(user.getUserId());
            return user.getUserId();
        }
        else {
            //请输入正确的用户名和密码
            return -1;
        }
    }

    //根据id查询用户信息
    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }

    //查询实名状态
    @Override
    public String SelectStoreStatus(Integer userId) {
        return userMapper.selectUserStatus(userId);
    }

    //用户实名认证
    @Override
    public String IdeafiUser(UserDto userDto) {
        User user=new User();
        BeanUtils.copyProperties(userDto, user);
//        System.out.println(merchantsDto.getMerchantName());
        user.setAccountStatus("待管理员审核");
        userMapper.updateById(user);
        return "认证成功";
    }

    //修改之前查询信息
    @Override
    public UserModifyVo SelectModify(Integer userId) {
        User user=userMapper.selectById(userId);
        UserModifyVo userModifyVo=new UserModifyVo();
        BeanUtils.copyProperties(user, userModifyVo);
        return  userModifyVo;
    }


    //修改个人信息
    @Override
    public String ModifyUser(UserModifyDto userModifyDto) {
//        System.out.println(userModifyDto.getUserId());
        String old_passwd=userModifyDto.getPasswd();
        String passwd=userMapper.selectPasswdById(userModifyDto.getUserId());
        User user=new User();
        String Message="";
        //包含修改密码和修改信息
        if(passwd.equals(old_passwd))
        {
            userModifyDto.setPasswd(userModifyDto.getNew_passwd());
            BeanUtils.copyProperties(userModifyDto,user);
            Message="成功修改密码和信息";
        }
        //密码错误不修改密码只修改信息
        else{
            userModifyDto.setPasswd(passwd);
            BeanUtils.copyProperties( userModifyDto,user);
            Message="信息修改成功,修改密码请输入正确的密码";
        }
        userMapper.updateById(user);
        return Message;
    }


    //
    //查询用户兼职记录
    @Override
    public List<UserPartTimeJobsVo> UserPartJob(Integer userId) {
        List<UserPartTimeJobsVo> userPartTimeJobsVoList=new ArrayList<>();
        // 创建 QueryWrapper 对象
        QueryWrapper<PartTimeJobs> queryWrapper = new QueryWrapper<>();
        // 设置条件，这里假设实体类中的属性为 id
        queryWrapper.eq("user_id", userId).and(q -> q.eq("job_status", "已录取").or().eq("job_status", "已解雇"));
        List<PartTimeJobs> partTimeJobs=partTimeJobsMapper.selectList(queryWrapper);
        for (PartTimeJobs partTimeJob : partTimeJobs) {
            UserPartTimeJobsVo userpartTimeJobsVo=new UserPartTimeJobsVo();
            int MerchantId=partTimeJob.getMerchantId();
           BeanUtils.copyProperties(partTimeJob, userpartTimeJobsVo);
            //查询商家表
            Merchants merchants=storeMapper.getById(MerchantId);
            BeanUtils.copyProperties(merchants, userpartTimeJobsVo);

            //查询评论表
            // 设置条件，这里假设实体类中的属性为 id
            QueryWrapper<Reviews> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("reviewer_id", userId).eq("reviewer_category", "用户").eq("target_id", userpartTimeJobsVo.getMerchantId());

            List<Reviews> reviews=reviewsMapper.selectList(queryWrapper1);

            if (reviews.size()!=0) {
                BeanUtils.copyProperties(reviews.get(0), userpartTimeJobsVo);
//                System.out.println(reviews.get(0).getRating());
//                System.out.println(userpartTimeJobsVo.getRating());
            }
            userPartTimeJobsVoList.add(userpartTimeJobsVo);
            }
        return userPartTimeJobsVoList;
    }


    @Override
    public String UserAddReview(UserAddReviewDto userAddReviewDto) {
        Reviews reviews=new Reviews();
        BeanUtils.copyProperties(userAddReviewDto,reviews);
        reviews.setTimestamps(new Date());
        reviewsMapper.insert(reviews);
        return "感谢您的评价!";
    }
}
